
//actions


import  commonUtils from  '../common/commonUtils'
import  state from  './state'
import axios from 'axios'

export  default {
  //提交留言
  submitMyMsg(context){
     //和后台交互， 进行保存。
      var loading = commonUtils.AlertLoading("正在读取你位置信息.....")
    var params ={
         content: state.currentMsgValue,
         nickName: state.name,
         email: state.email,
         avatar: state.avatar
    }
    axios.post('/msg/saveVisitorMsg',params)
         .then(function (success) {
            if (success.data.code == 0) {
              loading.close()
                   //将返回的数据，添加到列表的最前面。
              var oneMsg = success.data.result
              console.log(oneMsg)
                  context.commit("INSERTARR",{oneMsg})
               //成功将富文本内容清零
              state.currentMsgValue = ''
              commonUtils.AlertSuccessMsg("评论成功!!!")
            }
         })
         .catch(function (error) {
                console.log(error);
           commonUtils.AlertErrorMsg("提交评论失败！")
    })



  },
  increateNum(context){
    state.pageNum++;
  },

  // 初始化历史数据
  initPage(context){

    var  page={
       pageSize:state.pageSize,
       pageNum:state.pageNum
    }
    axios.post("/msg/getMsgHistoryPage",page).then(function (success) {
      if (success.data.code == 0) {
        var arr = success.data.result.dataList
        state.totalMsgPage=success.data.result.totalPage
        console.log("评论的总页数:"+success.data.result.totalPage)
        if (state.pageNum == 1) {
          state.visitorMsgList = arr
        }else {
          for(var i=0;i<arr.length;i++){
            state.visitorMsgList .push( arr[i]);
          }
        }

        state.scrollFlag=true;  //放这里就可以使避免异步导致数据混乱。
      }
    }).catch(function (error) {
         commonUtils.AlertErrorMsg("获取数据错误!!")
    })

  },

  //添加子留言
  saveChildReply(context,{msg,arrayIndex}){

      context.commit("SAVECHILDREPLY",{msg,arrayIndex})
  },

  //初始化数据
  initData(context){
    axios.post("/dic/initData").then((response)=>{
      if (response.data.code == 0) {
        state.lunbo = response.data.result.slideshow
        state.labelList = response.data.result.label;
      }
    }).catch((error) =>{
      commonUtils.AlertErrorMsg("初始化数据错误,请联系管理员。")
    })

  },

   //选项卡改变的 方法
  changeTab(){
    state.totalCount=0
      state.totalPage=1
      state.currentPage=1


  },

  //当每页显示数改变时。
  handleSizeChange(context,{pageSize}){
    state.bog_page_size = pageSize
    state.totalCount=0
    state.totalPage=1
    state.currentPage=1


  },

  //当点击上一页/下一页时,
  handleCurrentChange(context,{currentPage}){
    //判断当前页是否超过总的页数
    if (currentPage > state.totalPage || currentPage < 1) {
        commonUtils.AlertErrorMsg("请输入合法的页数!!!")
        return ;
    }
    state.currentPage = currentPage
  },

  //从store 中获取数据进行查询.
  selectPage(context){
    var loading = commonUtils.AlertLoading()
    var page={
        type:state.selectedValue,
        pageSize:state.bog_page_size,
        pageNum:state.currentPage
    }
      axios.post("/blog/selectPage",page).then((response) =>{
        if (response.data.code == 0) {
             state.cureentDataList =response.data.result.pageList;
             state.totalCount=response.data.result.totalCount;
             state.totalPage= Math.ceil(response.data.result.totalCount/state.bog_page_size)
          loading.close()
        }

      }).catch((error) =>{
        loading.close()
        commonUtils.AlertErrorMsg("获取数据失败,联系管理员!")
      })

  },

  // 改变标签
  turnToLabel(context,{value}){
    state.selectedValue=value
    state.totalCount=0
    state.totalPage=1
    state.currentPage=1
  },

  //抽屉搜索。
  search_keyword(context,keyword){

        state.drawer=true;
      axios.get("blog/search?keyword="+keyword).then(success =>{
        if (success.data.code == 0) {
          state.drawerSearchResult = success.data.result
          state.resultHit = success.data.result.length
          console.log("查询结果==>"+state.resultHit)

        }else {
          commonUtils.AlertOperationSuccess("查询失败!",1000)
        }
      }).catch(error =>{
          commonUtils.AlertOperationSuccess("查询失败!",1000)
      })
  },

  //加载指定文章的评论列表。
  loadingArticlCommentList(context,id){
     axios.post("msg/articleCommentList?id="+id).then(success =>{
       if (success.data.code == 0) {
         state.articleCommentList= success.data.result   //一次性加载完成。
       }else {
         commonUtils.AlertOperationSuccess("加载评论列表失败!",1000)
       }

     }).catch(error =>{
        commonUtils.AlertErrorMsg("加载评论列表失败，请联系管理员！")
     })
  },

  //评论文章
  submitArticleComment(context,obj){
    axios.post("msg/saveVisitorMsg",obj).then(success =>{
      if (success.data.code == 0) {
        console.log(state.articleCommentList)
        console.log("添加新增评论："+success.data.result)
         context.commit("ADDTOHEAD",success.data.result)
        console.log(state.articleCommentList)
        // 跳转到对应的评论处。
        document.querySelector("#comment").scrollIntoView(true);
        commonUtils.AlertOperationSuccess("评论成功!",1000)
      }else {
        commonUtils.AlertErrorMsg('评论失败,请联系管理员!')
      }
    }).catch(error =>{
      commonUtils.AlertErrorMsg('评论失败,请联系管理员!')
    })

  },
   //子评论
  submitChildReply(context,obj){
    axios.post("msg/saveVisitorMsg",obj).then(success =>{
      if (success.data.code == 0) {

        var re =success.data.result
        state.articleCommentList.splice(obj.arrayIndex, 1, re)
        // 跳转到对应的评论处。
        commonUtils.AlertOperationSuccess("评论成功!",1000)
        document.querySelector("#comment_"+obj.parent).scrollIntoView(true);
      }else {
        commonUtils.AlertErrorMsg('评论失败,请联系管理员!')
      }
    }).catch(error =>{
      commonUtils.AlertErrorMsg('评论失败,请联系管理员!')
    })
  },
  //获取上一篇下一篇
  getPreviousNext(context,id){
    console.log("当前的文章id=="+id+"==> 开始获取previous/next")
    axios.post("/blog/previousAndNext?id="+id).then(success =>{
      if (success.data.code == 0) {
        state.previous=success.data.result.previous;
        state.next=success.data.result.next;

      }else {
        commonUtils.AlertErrorMsg('获取上下篇文章失败!')
      }
    }).catch( error => {
      console.log('获取上下篇文章失败!')
    })

  },





}
